package com.chalk.common.util;

import   org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentStringPBEConfig;

/**
 * @author xiaoqiang
 * @Description Jasypt 加密工具类
 * @date 2019/3/21 18:48
 */
public class JasyptUtil {

    /**
     * 加密
     *
     * @param plaintext 待加密内容
     * @param salt      加密密码
     * @return
     */
    public static String encrypt(String plaintext, String salt) {
        /* 加密工具   */
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        /* 加密配置 */
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        /* 加密方式 */
        config.setAlgorithm("PBEWithMD5AndDES");
        /* 加密密码 */
        config.setPassword(salt);
        /* 应用配置 */
        encryptor.setConfig(config);
        return encryptor.encrypt(plaintext);
    }

    /**
     * 解密
     *
     * @param ciphertext 待解密内容
     * @param salt       解密密码
     * @return
     */
    public static String decrypt(String ciphertext, String salt) {
        /* 加密工具   */
        StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor();
        /* 加密配置 */
        EnvironmentStringPBEConfig config = new EnvironmentStringPBEConfig();
        /* 加密方式 */
        config.setAlgorithm("PBEWithMD5AndDES");
        /* 加密密码 */
        config.setPassword(salt);
        /* 应用配置 */
        encryptor.setConfig(config);
        return encryptor.decrypt(ciphertext);
    }



//    public static void main(String[] args) {
//        String salt = "mall-admin123!@#";
////        System.out.println(encrypt("com.mysql.jdbc.Driver", salt));
////        System.out.println(encrypt("jdbc:mysql://127.0.0.1/mall?useUnicode=true&amp;characterEncoding=UTF-8&useSSL=false", salt));
////        System.out.println(encrypt("root", salt));
////        System.out.println(encrypt("root", salt));
//
////        System.out.println(encrypt("com.mysql.jdbc.Driver", salt));
//
////        uzzHZLWn6FHcNN8jgdXOEGxiM1O3xh6c2vTrxXqMco/q825TAEqfo77whVxObc6kWjWVgme7agQImIHZL1E1u8Jqpn+O+Sdb4r51j8GA3XUIwkVLT2hQAlsL9aZCkS3WPsRC01kXo6Y=
////        System.out.println(encrypt("jdbc:mysql://120.79.35.251:3306/mall?useUnicode=true&amp;characterEncoding=UTF-8&useSSL=false", salt));
//
//
////        System.out.println(encrypt("jdbc:mysql://120.79.35.251:3306/mall_temp?useUnicode=true&amp;characterEncoding=UTF-8&useSSL=false", salt));
//
//
////        System.out.println(encrypt("chalktest", salt));
////        System.out.println(encrypt("Chalk!@#666", salt));
////        System.out.println("root---------------"+encrypt("root", salt));
////        System.out.println(encrypt("!@#456chalk", salt));
////        System.out.println("----------分割线-------------");
////        System.out.println(encrypt("jdbc:mysql://123.207.235.83:3306/mall?useUnicode=true&amp;characterEncoding=UTF-8&useSSL=false", salt));
////        System.out.println(encrypt("db_fish", salt));
////        System.out.println(encrypt("yiliang!@#456", salt));
////        System.out.println("----------本地-------------");
////        System.out.println(encrypt("jdbc:mysql://localhost:3306/mall?useUnicode=true&amp;characterEncoding=UTF-8&useSSL=false", salt));
////        System.out.println(encrypt("zhao", salt));
////        System.out.println(encrypt("Zhao1993..", salt));
////        System.out.println("----------一良-------------");
//        System.out.println(decrypt("VfNig4snxkDn7UoFRq7J8jKeMuD/OIcW",salt));
//        System.out.println(decrypt("8NF0DmgQ/MEuAnjifCJbBT0lKWbTapGXAbxjlmBE6z6b/wSFeFsoV/5SfoS4epXnY+JQqqra6DMyCcjQb6iQWZ2084Rfu4uhkq/IoGL3KXQwWeggYdSCXmwFPMq5jDdPBofO7HuUmGM=",salt));
//        System.out.println("---解密---"+decrypt("D30bHEJ68YC6zmFsJFpbva11koPoLOY560MMs6DBCXVNU4JV+DpejdUotBWSJWji/gLb9AE38d1xsxbt/zgAEt/fvVaNxMTjZcuckXsGXaXmcQf0rNSAs6CJ6Q3pZkbDNvjw/NbyHVX/QVVo797vWw==", salt));
//        System.out.println("---解密---"+decrypt("u/xjdrWA/KylNqtNB8syGQ==", salt));
//        System.out.println("---解密---"+decrypt("CpWjygIQozeXKO6K84DHKQ==", salt));
////        System.out.println(encrypt("jdbc:mysql://123.207.235.83:3306/mall_temp?useUnicode=true&amp;characterEncoding=UTF-8&useSSL=false", salt));
////        System.out.println(encrypt("zhao", salt));
////        System.out.println(encrypt("Zhao1993..", salt));
////
////
////        System.out.println(decrypt("D30bHEJ68YC6zmFsJFpbva11koPoLOY560MMs6DBCXVNU4JV+DpejdUotBWSJWji/gLb9AE38d1xsxbt/zgAEt/fvVaNxMTjZcuckXsGXaXmcQf0rNSAs6CJ6Q3pZkbDNvjw/NbyHVX/QVVo797vWw==", "mall-admin123!@#"));
//    }

}
